package com.haoxi.gateway.cmppmsg;

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.haoxi.gateway.util.TypeConvert;

public class CMPPConnectMessage extends CMPPMessage {

    private StringBuffer outBuf;

    public CMPPConnectMessage(String source_Addr, int version,
            String shared_Secret, Date timestamp)
            throws IllegalArgumentException {
        if (source_Addr == null)
            throw new IllegalArgumentException(
                    "The parameter of connect message is wrong:source_Addr value is null.");
        if (source_Addr.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of connect message is wrong:source_Addr length is larger than 6");
        if (version < 0 || version > 255)
            throw new IllegalArgumentException(
                    "The parameter of connect message is wrong:version less than 0 or larger than 255.");
        int len = 39;
        buf = new byte[len];
        TypeConvert.int2byte(len, buf, 0);
        TypeConvert.int2byte(CMPPCommon.CMPP_CONNECT, buf, 4);
        System.arraycopy(source_Addr.getBytes(), 0, buf, 12, source_Addr
                .length());
        if (shared_Secret != null)
            len = source_Addr.length() + 19 + shared_Secret.length();
        else
            len = source_Addr.length() + 19;
        byte tmpbuf[] = new byte[len];
        int tmploc = 0;
        System.arraycopy(source_Addr.getBytes(), 0, tmpbuf, 0, source_Addr
                .length());
        tmploc = source_Addr.length() + 9;
        if (shared_Secret != null) {
            System.arraycopy(shared_Secret.getBytes(), 0, tmpbuf, tmploc,
                    shared_Secret.length());
            tmploc += shared_Secret.length();
        }
        String tmptime = "0008080808";
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMddHHmmss");
        tmptime = dateFormat.format(timestamp);
        System.arraycopy(tmptime.getBytes(), 0, tmpbuf, tmploc, 10);
        md5(tmpbuf, 0, len, buf, 18);
        buf[34] = (byte) version;
        TypeConvert.int2byte(Integer.parseInt(tmptime), buf, 35);
        outBuf = new StringBuffer();
        outBuf.append(",source_Addr=").append(source_Addr);
        outBuf.append(",version").append(version);
        outBuf.append(",shared_Secret=").append(shared_Secret);
        outBuf.append(",timeStamp=").append(timestamp);
    }

    public String toString() {
        StringBuffer tmpBuf = new StringBuffer("CMPP_Connect: ");
        tmpBuf.append("Sequence_Id=").append(getSequenceId()).append(
                outBuf.toString());
        return tmpBuf.toString();
    }

    public static void md5(byte data[], int offset, int length, byte digest[],
            int dOffset) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(data, offset, length);
            md5.digest(digest, dOffset, 16);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (DigestException ex) {
            ex.printStackTrace();
        }
    }

    public int getCommandId() {
        return CMPPCommon.CMPP_CONNECT;
    }
}
